package org.esk.dablog.web.forms.forum;

import org.esk.dablog.web.forms.AbstractAddComment;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.model.Comment;
import org.esk.dablog.model.Entry;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.validation.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class implements "add topic" task
 * User: esk
 * Date: 08.12.2006
 * Time: 20:26:31
 * $Id:$
 */
public class ReplyCommentForm extends AbstractAddComment {


    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        super.onSubmit(request, response, command, errors);
        Comment c = (Comment) command;
        return new ModelAndView(new RedirectView(getSuccessView() + "/" + c.getEntry().getId()));
    }

    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        Comment c = new Comment();
        long commentId = RequestUtils.extractLongParameter(request);
        Comment replyTo = getEntryManager().getComment(commentId);
        c.setEntry(replyTo.getEntry());
        c.setParent(replyTo);
        return c;
    }
}
